/*
 * Jenny Ruppen 09-730-391
 * Sebastien Goldberg 08-914-855
 * Henry Raymond 08-928-061
 * Stefan Mussato 09-752-270
 */
 
package ch.uzh.ifi.sopra.gruppe9.game;

import java.util.ArrayList;
import com.jme3.scene.Node;


/**
 * Interactive in-game screen of the cheat window.
 */
public class CheatScreen extends Node {
	
	
	/**
	 * List of all registered mouseOver event elements of the cheat screen.
	 */
	private ArrayList<IMouseOverable> mouseOverables = new ArrayList<IMouseOverable>();
	
	
	/**
	 * List of all registered click event elements of the cheat screen.
	 */
	private ArrayList<IClickable> clickables = new ArrayList<IClickable>();
	
	
	/**
	 * Text telling player that it's not his turn
	 */
	private GuiText notYourTurn;
	
	
	/**
	 * Geometry node of all content of the window.
	 */
	private Node cheatScreen;

	
	/**
	 * Set season to winter.
	 */
	private GuiButton jumpToWinter;
	
	
	/**
	 * Set season to spring.
	 */
	private GuiButton jumpToSpring;
	
	
	/**
	 * Set season to summer.
	 */
	private GuiButton jumpToSummer;
	
	
	/**
	 * Set season to autumn.
	 */
	private GuiButton jumpToAutumn;
	
	
	/**
	 * Start end-of-season score calculation.
	 */
	private GuiButton endOfSeasonScore;
	
	
	/**
	 * Start end-of-game score calculation.
	 */
	private GuiButton endOfGameScore;
	
	
	/**
	 * Gives tower elements to the player.
	 */
	private GuiButton getTowerElements;
	
	
	/**
	 * Gives towers to the player.
	 */
	private GuiButton getTowers;
	
	
	/**
	 * Gives player money.
	 */
	private GuiButton getMoney;
	
	
	/**
	 * Gives player points.
	 */
	private GuiButton getPoints;
	
	
	/**
	 * Close window button
	 */
	private GuiButton close;

	
	/**
	 * Constructor: Creates geometry.
	 */
	public CheatScreen() {
		
		cheatScreen = new Node();
		
		GuiWindow window = new GuiWindow(GUI.Offset.SCENE, 29, 30, 710, 610, 50);
		cheatScreen.attachChild(window);
		
		GuiText title = new GuiText("Cheats", Fonts.getLarge());
		title.setPositionSize(GUI.Offset.SCENE, (768 - (int)title.getLineWidth()) / 2, 60, 51);
		cheatScreen.attachChild(title);
		
		jumpToWinter = new GuiButton("Winter");
		jumpToWinter.setPositionSize(GUI.Offset.SCENE, ((768 - 660) / 2) + 0, 120, 150, 51);
		jumpToWinter.setAction(new ICallback() { 
			public void run() {
				Game.board().cheat(Game.board().getMyIndex(), 1);
				GUI.changeState(GuiState.State.CLEAN_INGAME_SCREEN);
			}
		});
		cheatScreen.attachChild(jumpToWinter.create());
		
		jumpToSpring = new GuiButton("Fr\u00FChling");
		jumpToSpring.setPositionSize(GUI.Offset.SCENE, ((768 - 660) / 2) + 170, 120, 150, 51);
		jumpToSpring.setAction(new ICallback() { 
			public void run() {
				Game.board().cheat(Game.board().getMyIndex(), 2);
				GUI.changeState(GuiState.State.CLEAN_INGAME_SCREEN);
			}
		});
		cheatScreen.attachChild(jumpToSpring.create());
		
		jumpToSummer = new GuiButton("Sommer");
		jumpToSummer.setPositionSize(GUI.Offset.SCENE, ((768 - 660) / 2) + 340, 120, 150, 51);
		jumpToSummer.setAction(new ICallback() { 
			public void run() {
				Game.board().cheat(Game.board().getMyIndex(), 3);
				GUI.changeState(GuiState.State.CLEAN_INGAME_SCREEN);
			}
		});
		cheatScreen.attachChild(jumpToSummer.create());
		
		jumpToAutumn = new GuiButton("Herbst");
		jumpToAutumn.setPositionSize(GUI.Offset.SCENE, ((768 - 660) / 2) + 510, 120, 150, 51);
		jumpToAutumn.setAction(new ICallback() { 
			public void run() {
				Game.board().cheat(Game.board().getMyIndex(), 4);
				GUI.changeState(GuiState.State.CLEAN_INGAME_SCREEN);
			}
		});
		cheatScreen.attachChild(jumpToAutumn.create());
		
		
		endOfSeasonScore = new GuiButton("Jahreszeitauswertung starten");
		endOfSeasonScore.setPositionSize(GUI.Offset.SCENE, (768 - 300) / 2, 180, 300, 51);
		endOfSeasonScore.setAction(new ICallback() { 
			public void run() {
				Game.board().cheat(Game.board().getMyIndex(), 5);
				GUI.changeState(GuiState.State.CLEAN_INGAME_SCREEN);
			}
		});
		cheatScreen.attachChild(endOfSeasonScore.create());
		
		
		endOfGameScore = new GuiButton("Endauswertung starten");
		endOfGameScore.setPositionSize(GUI.Offset.SCENE, (768 - 300) / 2, 240, 300, 51);
		endOfGameScore.setAction(new ICallback() { 
			public void run() {
				Game.board().cheat(Game.board().getMyIndex(), 6);
				GUI.changeState(GuiState.State.CLEAN_INGAME_SCREEN);
			}
		});
		cheatScreen.attachChild(endOfGameScore.create());
		
		
		getTowerElements = new GuiButton("Gib mir Turmteile!");
		getTowerElements.setPositionSize(GUI.Offset.SCENE, (768 - 300) / 2, 300, 300, 51);
		getTowerElements.setAction(new ICallback() { 
			public void run() {
				Game.board().cheat(Game.board().getMyIndex(), 7);
				GUI.changeState(GuiState.State.CLEAN_INGAME_SCREEN);
			}
		});
		cheatScreen.attachChild(getTowerElements.create());
		
		getTowers = new GuiButton("Gib mir T\u00FCrme!");
		getTowers.setPositionSize(GUI.Offset.SCENE, (768 - 300) / 2, 360, 300, 51);
		getTowers.setAction(new ICallback() { 
			public void run() {
				Game.board().cheat(Game.board().getMyIndex(), 8);
				GUI.changeState(GuiState.State.CLEAN_INGAME_SCREEN);
			}
		});
		cheatScreen.attachChild(getTowers.create());
		
		getMoney = new GuiButton("Gib mir Geld!");
		getMoney.setPositionSize(GUI.Offset.SCENE, (768 - 300) / 2, 420, 300, 51);
		getMoney.setAction(new ICallback() { 
			public void run() {
				Game.board().cheat(Game.board().getMyIndex(), 9);
				GUI.changeState(GuiState.State.CLEAN_INGAME_SCREEN);
			}
		});
		cheatScreen.attachChild(getMoney.create());
		
		
		getPoints = new GuiButton("Gib mir Punkte!");
		getPoints.setPositionSize(GUI.Offset.SCENE, (768 - 300) / 2, 480, 300, 51);
		getPoints.setAction(new ICallback() { 
			public void run() {
				Game.board().cheat(Game.board().getMyIndex(), 10);
				GUI.changeState(GuiState.State.CLEAN_INGAME_SCREEN);
			}
		});
		cheatScreen.attachChild(getPoints.create());
		
		
		notYourTurn = new GuiText("Du bist nicht am Zug.");
		notYourTurn.setPositionSize(GUI.Offset.SCENE, (768 - (int)notYourTurn.getLineWidth()) / 2, 528, 51);
		
		
		close = new GuiButton("Schliessen");
		close.setPositionSize(GUI.Offset.SCENE, (768 - 220) / 2, 555, 220, 51);
		close.setAction(new ICallback() { 
			public void run() { 
				GUI.changeState(GuiState.State.CLEAN_INGAME_SCREEN);
			}
		});
		cheatScreen.attachChild(close.create());
		
		clickables.add(jumpToWinter);
		clickables.add(jumpToSpring);
		clickables.add(jumpToSummer);
		clickables.add(jumpToAutumn);
		clickables.add(endOfSeasonScore);
		clickables.add(endOfGameScore);
		clickables.add(getTowerElements);
		clickables.add(getTowers);
		clickables.add(getMoney);
		clickables.add(getPoints);
		clickables.add(close);
		
		mouseOverables.add(jumpToWinter);
		mouseOverables.add(jumpToSpring);
		mouseOverables.add(jumpToSummer);
		mouseOverables.add(jumpToAutumn);
		mouseOverables.add(endOfSeasonScore);
		mouseOverables.add(endOfGameScore);
		mouseOverables.add(getTowerElements);
		mouseOverables.add(getTowers);
		mouseOverables.add(getMoney);
		mouseOverables.add(getPoints);
		mouseOverables.add(close);
		
	}
	
	
	/**
	 * Attach all needed geometry and register all mouseOver and click events.
	 */
	public void show() {
		
		this.attachChild(cheatScreen);
		GUI.registerMouseOverable(mouseOverables);
		GUI.registerClickable(clickables);
		
		updateCheatScreen();
	}
	
	
	/**
	 * Detach all geometry and unregister all mouseOver and click events.
	 */
	public void hide() {
		this.detachChild(cheatScreen);
		
		GUI.unregisterMouseOverable(mouseOverables);
		GUI.unregisterClickable(clickables);
	}
	
	
	/**
	 * Updates content of the cheat screen.
	 */
	public void updateCheatScreen() {
		if(Game.board().getTurn() == Game.board().getMyIndex()) {
			if(cheatScreen.hasChild(notYourTurn)) {
				cheatScreen.detachChild(notYourTurn);
			}
			jumpToWinter.setActive(true);
			jumpToSpring.setActive(true);
			jumpToSummer.setActive(true);
			jumpToAutumn.setActive(true);
			endOfSeasonScore.setActive(true);
			endOfGameScore.setActive(true);
			getTowerElements.setActive(true);
			getTowers.setActive(true);
			getMoney.setActive(true);
			getPoints.setActive(true);
		} else {
			if(!cheatScreen.hasChild(notYourTurn)) {
				cheatScreen.attachChild(notYourTurn);
			}
			jumpToWinter.setActive(false);
			jumpToSpring.setActive(false);
			jumpToSummer.setActive(false);
			jumpToAutumn.setActive(false);
			endOfSeasonScore.setActive(false);
			endOfGameScore.setActive(false);
			getTowerElements.setActive(false);
			getTowers.setActive(false);
			getMoney.setActive(false);
			getPoints.setActive(false);
		}
	}
	
}
